package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

public class UsernameExistServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        AccountService accountService = new AccountService();
        if (Objects.equals(username, "")) {
            resp.setContentType("text/plain");
            PrintWriter out = resp.getWriter();
            out.print("null");
            out.flush();
            out.close();
            return;
        }
        Account account = accountService.getAccount(username);
        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();
        if (account == null) {
            out.print("Not Exist");
        } else {
            out.print("Exist");
        }
        out.flush();
        out.close();
    }
}
